{include file="common/header"/}

<body class="bg-light">
	<div class="container">
		<div class="col-md-4 offset-md-4 mt-md-3">
			<div class="card">
				<div class="card-body" >
			    	<h4 class="card-title">登录</h4>
			    	<hr>
			    	<form id="login_form" novalidate>
						<div class="form-group">
							<label for="login_username">用户名</label>
	          				<input id="login_username" type="text" class="form-control" placeholder="请输入用户名">
	          				<div id="login_username_feedback" class="invalid-feedback"><small></small></div>
						</div>
						<div class="form-group">
							<label for="login_password">密码</label>
				          	<input id="login_password" type="password" class="form-control" placeholder="请输入密码">
				          	<div id="login_password_feedback" class="invalid-feedback"><small></small></div>
				        </div>
						<div class="form-group">
							<button id="login_submit" type="submit" class="btn btn-primary btn-block">立即登录</button>
						</div>
					</form>
			  	</div>
			</div>
		</div>
	</div>
</body>

<script type="text/javascript">
	$(function () {
		$('#login_username').blur(check_username);
		function check_username() {
			var username_value = $('#login_username').val();

			if(username_value == '') {
				$('#login_username_feedback').html('用户名不能为空');
				$('#login_username').addClass('is-invalid');
				return false;
			}
			else {
				$('#login_username').removeClass('is-invalid');
				return true;
			}
		}
		$('#login_password').blur(check_password);
		function check_password() {
			var password_value = $('#login_password').val();

			if (password_value == '') {
				$('#login_password_feedback').html('密码不能为空');
				$('#login_password').addClass('is-invalid');
				return false;
			}
			else {
				$('#login_password').removeClass('is-invalid');
				return true;
			}
		}
    	$("#login_form").submit(function(event){
    		event.preventDefault();
    		var is_username_valid = check_username();
    		var is_password_valid = check_password();
    		// 验证成功发送消息
    		if (is_username_valid && is_password_valid) {
				var username = $('#login_username').val();
     			var password = $('#login_password').val();
				$.post("/index/login/login", {
	      			'username': username,
	      			'password': password
	      		}, 
	      		function(result){
	      			if (result.status == 'success') {
	      				toastr.success(result.message);
	      				setTimeout(function(){
	      					$(location).attr('href', result.data);
						}, 1000);
	      			}
	      			if (result.status == 'error') {
	      				toastr.error(result.message);
	      			}	      			
	      		}, "json");
    		}
  		})
    })
</script>